import time

import requests, json
import datetime
###########################

actId = 2405280246586894  # 霍乱后宫的瓜6
actGoodId = 537183453
# 6月1日（周六）19:30法扎签售接龙
Authorization = "eyJhbGciOiJIUzUxMiJ9.eyJ1aWQiOjE4MzAxODU4OSwiZXhwIjoxNzE3NjYwOTQ5fQ.w4w-yDGtKFE3tgnFDRqhHHfepfj0w_ZmyOi8jvKJl2I3jwxmGf2MG4vYBaWXvnjTnr0auADCW_eHphUVM_92PA"
proxies = {"http": None, "https": None}
logname = "bm_hlhgdg6.log"
###########################
while datetime.datetime.now() < datetime.datetime(2024, 6, 1, 17, 30, 0):
    pass

goodsUrl = "https://apipro.qunjielong.com/activity-biz/activity/detail/goods"
data = {
  "actId": actId
}
headers = {
    "Host": "apipro.qunjielong.com",
    "Connection": "keep-alive",
    "device-type": "10",
    "mini-route": "pro/pages/generate-order/generate-order",
    "Authorization": Authorization,
    "uid": "182893922",
    "companyId": "190",
    "scene-flow": "seq-detail,8f15647b333a5fcda597696b51e2dfe0",
    "client-version": "5.6.7",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a1b) XWEB/9129",
    "Content-Type": "application/json",
    "xweb_xhr": "1",
    "appid": "wx059cd327295ab444",
    "sceneCode": "1007",
    "version": "3.0.0",
    "Accept": "*/*",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://servicewechat.com/wx059cd327295ab444/1784/page-frame.html",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9"
}

place_all = "https://apipro.qunjielong.com/order-place/api/mina/order_place/place-all"


def run(f_w):
    try:
        # data = {
        #     "signUpOrderDTO": {
        #         "userInfo": {},
        #         "proOrderItemParamList": [{
        #             "actGoodId": actGoodId,
        #             "buyQuantity": 1
        #         }],
        #         "dlvAddrId": 0,
        #         "proOrderDefExts": [],
        #         "markInfoJson": {
        #             "markInfoList": [{
        #                 "type": "text",
        #                 "content": [cdesc]
        #             }]
        #         },
        #         "orderDefItemList": []
        #     },
        #     "actId": actId
        # }

        data = {
            "signUpOrderDTO": {
                "userInfo": {},
                "proOrderItemParamList": [{
                    "actGoodId": actGoodId,
                    "buyQuantity": 1
                }],
                "dlvAddrId": 0,
                "proOrderDefExts": [],
                "markInfoJson": {},
                "orderDefItemList": []
            },
            "actId": actId
        }
        res = requests.post(place_all, headers=headers, data=json.dumps(data), proxies=proxies)
        print(res.text)
        f_w.write(json.dumps(res.json(), indent=4, ensure_ascii=False, sort_keys=True))
        f_w.write("\n")
        orderNo = res.json().get("data").get("orderNo")
        query_order_pay_success_toast_list = "https://apipro.qunjielong.com/order-place/api/mina/order_place/query_order_pay_success_toast_list?actId={}&orderNo={}".format(
            actId, orderNo)
        res = requests.get(query_order_pay_success_toast_list, headers=headers, proxies=proxies)
        print(res.text)
        f_w.write(json.dumps(res.json(), indent=4, ensure_ascii=False, sort_keys=True))
        f_w.write("\n")
        if res.json().get("success"):
            return True
    except Exception as e:
        print(e)
        return False


with open(logname, "w", encoding="utf-8") as f_w:
    for i in range(10):
        f_w.write(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") + "\n")
        if run(f_w):
            break
        time.sleep(0.2)

